﻿using Mapster;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Service.Auth.Api.Application.Query;
using Service.Core.Identity.Model;
using Service.Core.Repository.Service;
using Service.Framework.OperationLog;
using Service.Framework.Packet;
using Service.Auth.Api.Application.Command;
using Service.Auth.Model.Model.Dto;
using Service.Auth.Model.Model.RequestDto.System;
using Service.Auth.Model.Model.Vo;

namespace Service.Auth.Api.Controller
{
    /// <summary>
    /// 系统管理
    /// </summary>
    public class SystemController(
        IMediator mediator,
        SystemQueryService systemQuery, IOperationLogService operationLogService
        ) : BaseController(mediator)
    {
        /// <summary>
        /// 创建系统
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Policy = AuthPolicy.Permission)]
        public async Task<bool> CreateSystem([FromBody] CreateSystemDto dto)
        {
            var command = new CreateSystemCommand(dto);
            return await _mediator.Send(command);
        }


        /// <summary>
        /// 通过客户端id查询系统数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize(AuthPolicy.Inner)]
        public async Task<TSystemVo> InnerQuerySystemByClientId([FromQuery] string clientId)
        {
            return await systemQuery.QuerySystemByClientId(clientId);
        }

        /// <summary>
        /// 通过客户端id查询系统数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<IdName<long, string>>> QuerySystemDropdown()
        {
            return await systemQuery.QuerySystemDropdown();
        }


        /// <summary>
        /// 查询操作日志
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Policy = AuthPolicy.Permission)]
        public async Task<PagedList<OperationLogDto>> QueryOperationLogs([FromQuery] PageRequestDto page, string userId)
        {
            var data = await operationLogService.GetOperationLogs(page.PageIndex, page.PageSize, Service.Framework.OperationLog.OperationLogType.Operation, userId);
            var items = data.list.Adapt<List<OperationLogDto>>();
            return new PagedList<OperationLogDto>(items, page.PageIndex, page.PageSize, data.totalCount); ;
        }


    }
}